package com.dtda.edu.miaosha.service.impl;

import com.dtda.edu.miaosha.bo.ItemBo;
import com.dtda.edu.miaosha.bo.MarketBo;
import com.dtda.edu.miaosha.bo.OrderBo;
import com.dtda.edu.miaosha.dao.ItemPoMapper;
import com.dtda.edu.miaosha.dao.MarketPoMapper;
import com.dtda.edu.miaosha.dao.OrderPoMapper;
import com.dtda.edu.miaosha.dao.UserInfoPoMapper;
import com.dtda.edu.miaosha.po.ItemPo;
import com.dtda.edu.miaosha.po.MarketPo;
import com.dtda.edu.miaosha.po.OrderPo;
import com.dtda.edu.miaosha.po.UserInfoPo;
import com.dtda.edu.miaosha.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 袁强
 * @data 2019/5/14 9:23
 * @Description:
 */
@Service("orderService")
public class OrderServiceImpl  implements OrderService{

    @Autowired
    private OrderPoMapper orderPoMapper;
    @Autowired
    private ItemPoMapper itemPoMapper;
    @Autowired
    private UserInfoPoMapper userInfoPoMapper;
    @Autowired
    private MarketPoMapper marketPoMapper;

    @Override
    public List<OrderBo> showOrders() {
        ArrayList<OrderBo> orders = new ArrayList<OrderBo>();
        List<OrderPo> orderPos = orderPoMapper.selectOrders();
        for (OrderPo orderPo:orderPos) {
            OrderBo orderBo = new OrderBo();
            BeanUtils.copyProperties(orderPo,orderBo);
            orderBo.setOrderPrice(BigDecimal.valueOf(orderPo.getOrderPrice()));
            ItemPo itemPo = itemPoMapper.selectByPrimaryKey(orderPo.getItemId());
            ItemBo itemBo = new ItemBo();
            BeanUtils.copyProperties(itemPo,itemBo);
            orderBo.setItemBo(itemBo);
            orders.add(orderBo);
        }
        return orders;
    }

    @Override
    public boolean deleteOrder(String orderId) {
        int i = orderPoMapper.deleteByPrimaryKey(orderId);
        if(i > 0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public OrderBo selectOrderById(String orderId) {
        OrderBo orderBo = new OrderBo();
        OrderPo orderPo = orderPoMapper.selectByPrimaryKey(orderId);
        ItemPo itemPo = itemPoMapper.selectByPrimaryKey(orderPo.getItemId());
        ItemBo itemBo = new ItemBo();
        BeanUtils.copyProperties(itemPo,itemBo);
        MarketPo marketPo = marketPoMapper.selectByItemId(orderPo.getMarketId());
        if(marketPo != null){
            MarketBo marketBo = new MarketBo();
            BeanUtils.copyProperties(marketPo,marketBo);
            orderBo.setMarketBo(marketBo);
        }
        UserInfoPo userInfoPo = userInfoPoMapper.selectByPrimaryKey(orderPo.getUserId());
        BeanUtils.copyProperties(orderPo,orderBo);
        orderBo.setItemBo(itemBo);
        orderBo.setUserInfoPo(userInfoPo);
        return orderBo;
    }
}
